<?php
class FindBijiTerima{
	private $subbagian;
	private $label;
	private $db;
	
	function FindBijiTerima($db=NULL, $subbagian=NULL, $label=NULL){
		$this->subbagian = $subbagian;
		$this->label 	 = $label;
		$this->db 		 = $db;
	}
	
	private function findPencacahan(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_pencacahan` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findDepericarpingDanPolishing(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_depericarping_dan_polishing` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findPencucian(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_pencucian` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findPengeringan(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_pengeringan` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findSeleksiAwal(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_seleksi_awal` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findRuangPanas(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_ruang_panas` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findRuangDingin(){
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_ruang_dingin` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findForGerminasi(){ // either from perendaman, or ruang panas
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_perendaman` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	private function findGerminasi(){ // either from perendaman, or ruang panas
		$q = "SELECT `jml_biji_seleksi` FROM `caramel_operational`.`inv_germinasi` WHERE `label_tandan` LIKE '".$this->label."'";
		$r = $this->db->get_var($q);
		
		if ( !($r > 0) ) $r = 0;
		echo json_encode(array('jml_biji_terima'=>$r));
	}
	
	function response(){
		switch($this->subbagian){
			case 'inv_depericarping_dan_polishing':
				$this->findPencacahan();
				break;
			case 'inv_pencucian':
				$this->findDepericarpingDanPolishing();
				break;
			case 'inv_pengeringan':
				$this->findPencucian();
				break;
			case 'inv_seleksi':
				$this->findPengeringan();
				break;
			case 'inv_ruang_panas':
				$this->findSeleksiAwal();
				break;
			case 'inv_ruang_dingin':
				$this->findRuangPanas();
				break;
			case 'inv_perendaman':
				$this->findRuangDingin();
				break;
			case 'inv_germinasi':
				$this->findForGerminasi(); // special, wae...
				break;
			case 'inv_seleksi_akhir':
				$this->findGerminasi();
				break;			
		}
	}	
}

$subbagian 	= $_GET['subbagian'];
$label		= $_GET['label_tandan'];


// load database class handler
include_once '../config/db_config.php';
include '../library/ezsql/shared/ez_sql_core.php';
include '../library/ezsql/mysql/ez_sql_mysql.php';

// database handler
$db = new ezSQL_mysql($dbUser, $dbPass, $dbName, $dbHost);


$finder = new FindBijiTerima($db, $subbagian, $label);
$finder->response();
?>